package com.kotlin.zds.utils

import android.Manifest
import android.app.Activity
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.zhouwei.library.CustomPopWindow
import com.github.dhaval2404.imagepicker.ImagePicker
import com.kotlin.zds.R

import pub.devrel.easypermissions.EasyPermissions
import java.io.File
import java.util.*

/**
 * 照片选择
 */
class SelectPhotoUtil(val mActivity: Activity) {
    var popwindow: CustomPopWindow? = null
    companion object {
        val permissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
        const val REQUEST_CODE_PICK_AVATER = 0X02
    }

    //选择弹出框
    fun select() {
        if (EasyPermissions.hasPermissions(mActivity, *permissions)) {
            val contentView: View = LayoutInflater.from(mActivity).inflate(R.layout.pop_select_phopo, null)
            contentView.findViewById<TextView>(R.id.btnGallery).setOnClickListener { startGallery() }
            contentView.findViewById<TextView>(R.id.btnCamera).setOnClickListener { startCamera() }
            contentView.findViewById<TextView>(R.id.btnCancel).setOnClickListener { popwindow!!.dissmiss() }

            popwindow = CustomPopWindow.PopupWindowBuilder(mActivity)
                    .setView(contentView)
                    .size(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
                    .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                    .setAnimationStyle(0)
                    .setBgDarkAlpha(0.7f) // 控制亮度
                    .create()
                    .showAtLocation(mActivity.window.decorView, Gravity.BOTTOM, 0, 0)
        } else {
            EasyPermissions.requestPermissions(mActivity, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions)
        }
    }

    //打开相册
    fun startGallery() {
        if (EasyPermissions.hasPermissions(mActivity, *permissions)) {
            popwindow!!.dissmiss()
            ImagePicker.with(mActivity)
                    .galleryOnly()
                    .crop()
                    .compress(1024)
                    .maxResultSize(512, 512)
                    //  Path: /data/data/package/cache/ImagePicker
                    .saveDir(File(mActivity!!.getCacheDir(), String.format(Locale.CHINA, "%s.png", System.currentTimeMillis())))
                    .start(REQUEST_CODE_PICK_AVATER)
        } else {
            mActivity.let { EasyPermissions.requestPermissions(it, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions) }
        }
    }

    //打开相机
    fun startCamera() {
        if (EasyPermissions.hasPermissions(mActivity!!, *permissions)) {
            popwindow!!.dissmiss()
            ImagePicker.with(mActivity!!)
                    .cameraOnly()
                    .crop()
                    .compress(1024)
                    .maxResultSize(512, 512)
                    //  Path: /data/data/package/cache/ImagePicker
                    .saveDir(File(mActivity!!.getCacheDir(), String.format(Locale.CHINA, "%s.png", System.currentTimeMillis())))
                    .start(REQUEST_CODE_PICK_AVATER)
        } else {
            mActivity.let { EasyPermissions.requestPermissions(it, mActivity!!.getString(R.string.permissions_request_pick_avater), 1, *permissions) }
        }
    }

}